/*
#Copyright (c) 2013, Philippe Bordron <philippe.bordron@gmail.com>
#
# This file is part of SIPPER.
#
# DCC is free software: you can redistribute it and/or modify
# it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DCC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU LESSER GENERAL PUBLIC LICENSE for more details.
#
# You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE
# along with SIPPER.  If not, see <http://www.gnu.org/licenses/>
*/
package combi.wip.sipper.exec;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CompareSimple {

	private static String tag = "(?:([^\t]+)\t+([^\t]+)\t+([^\t]+))";
	private static String tag2 = "(?:(.+)->(.+))";

	public static String compare(String s1, String s2)
	{
		String result = "";
		String[] ss1 = s1.split("\n");
		String[] ss2 = s2.split("\n");
		if (ss1.length == ss2.length)
		{
			for(int i= 0; i<ss1.length; i++)
			{
				result = result + ss1[i] + "\n" +ss2[i] + "\n----------------------------------\n";
			}
		}
		else if (ss1.length < ss2.length)
		{
			int i= 0;
			for(i= 0; i<ss1.length; i++)
			{
				result = result + ss1[i] + "\n" +ss2[i] + "\n----------------------------------\n";
			}
			for (int j =i ; j<ss2.length; j++)
			{
				result = result +ss2[i] + "\n----------------------------------\n";
			}
		}
		else
		{
			int i= 0;
			for(i= 0; i<ss2.length; i++)
			{
				result = result + ss1[i] + "\n" +ss2[i] + "\n----------------------------------\n";
			}
			for (int j =i ; j<ss1.length; j++)
			{
				result = result +ss1[i] + "\n----------------------------------\n";
			}
		}
		return result;
	}

	public static Map<String, String> load (String fileIn) throws IOException
	{
		Map<String, String> result = new HashMap<String,String>();
		BufferedReader in;
		in = new BufferedReader (new FileReader (fileIn));
		Pattern recoHead = Pattern.compile(tag2);
		Matcher m2;

		Pattern reco = Pattern.compile(tag);
		Matcher m;
		String ch;
		String head = "";
		String bloc = "";
		boolean hasHead = false;
		while((ch = in.readLine())!=null)
		{
			m2 = recoHead.matcher(ch);
			if (m2.find())
			{
				if (hasHead)
				{
					result.put(head,bloc);
				}
				head = ch;
				bloc = "";
				hasHead = true;
			}
			m = reco.matcher(ch);
			if (m.find())
			{
				bloc += ch + "\n";
			}
		}if (hasHead)
		{
			result.put(head,bloc);
		}
		in.close();
		return result;
	}
	
	public static void main(String[] args) throws IOException
	{
		String fileIn1 = args[0];
		String fileIn2 = args[1];
		String fileOut = args[2];
		Map<String, String> map1 = load(fileIn1);
		Map<String, String> map2 = load(fileIn2);

		
		Set<String> common = new HashSet<String>(map1.keySet());
		common.retainAll(map2.keySet());
		System.out.println("Common k-SIPs id : " + common.size());
		Set<String> s1minus2 = new HashSet<String>(map1.keySet());
		s1minus2.removeAll(map2.keySet());
		System.out.println("File 1 exclusive k-SIPs id : " + s1minus2.size());
		Set<String> s2minus1 = new HashSet<String>(map2.keySet());
		s2minus1.removeAll(map1.keySet());
		System.out.println("File 2 exclusive k-SIPs id : " + s2minus1.size());
		

		BufferedWriter out = new BufferedWriter (new FileWriter (fileOut));
		for (String s : common)
		{
			out.write(s+"\n"+compare(map1.get(s), map2.get(s)) + "\n");
		}
		out.write("\n*********************************\n");
		for (String s : s1minus2)
		{
			out.write(s+"\n"+map1.get(s)+ "\n");
		}
		out.write("\n*********************************\n");
		for (String s : s2minus1)
		{
			out.write(s+"\n"+ map2.get(s) + "\n");
		}
		
		out.close();
	}

}
